﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchContract : UserControl, InterfaceSearchControl
    {
        private bool _isNotFill = true;

        private bool _openOnDblClick = false;
        
        public UserControlSearchContract()
        {
            InitializeComponent();
        }

        public void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewTextBoxColumn colType = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.TYPE_DOCColumn.ColumnName);
            colType.HeaderText = "Тип";

            GridViewTextBoxColumn colOrg = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.SHORT_NAMEColumn.ColumnName);
            colOrg.HeaderText = "Заказчик";

            GridViewDateTimeColumn colStartDate = new GridViewDateTimeColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.START_DATEColumn.ColumnName);
            colStartDate.HeaderText = "Дата начала";
            colStartDate.FormatString = "{0:d}";

            GridViewTextBoxColumn colGroups = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.PROD_GROUPSColumn.ColumnName);
            colGroups.HeaderText = "Группы товаров";

            //GridViewDateTimeColumn colEndDate = new GridViewDateTimeColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.END_DATEColumn.ColumnName);
            //colEndDate.HeaderText = "Дата окончания";
            //colEndDate.FormatString = "{0:d}";
            //Колонки для создания иерархии
            GridViewTextBoxColumn colID = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.IDColumn.ColumnName);
            colID.IsVisible = false;
            colID.VisibleInColumnChooser = false;
            GridViewTextBoxColumn colParID = new GridViewTextBoxColumn(dataSetFSO1.VIEW_SEARCH_CONTRACT.PARENT_CONTRACT_IDColumn.ColumnName);
            colParID.IsVisible = false;
            colParID.VisibleInColumnChooser = false;

            
            radGridViewSearchContract.MasterTemplate.Columns.Clear();
            radGridViewSearchContract.MasterTemplate.Columns.Add(colNumber);
            radGridViewSearchContract.MasterTemplate.Columns.Add(colType);
            radGridViewSearchContract.MasterTemplate.Columns.Add(colOrg);
            radGridViewSearchContract.MasterTemplate.Columns.Add(colStartDate);
            //radGridViewSearchContract.MasterTemplate.Columns.Add(colEndDate);

            radGridViewSearchContract.MasterTemplate.Columns.Add(colID);
            radGridViewSearchContract.MasterTemplate.Columns.Add(colParID);
            radGridViewSearchContract.MasterTemplate.Columns.Add(colGroups);
            //vieW_SEARCH_CONTRACTTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_CONTRACT);

            radGridViewSearchContract.DataSource = dataSetFSO1.VIEW_SEARCH_CONTRACT;

            radGridViewSearchContract.Relations.Clear();
            radGridViewSearchContract.Relations.AddSelfReference(radGridViewSearchContract.MasterTemplate, dataSetFSO1.VIEW_SEARCH_CONTRACT.IDColumn.ColumnName, dataSetFSO1.VIEW_SEARCH_CONTRACT.PARENT_CONTRACT_IDColumn.ColumnName);
        }

        #region InterfaceSearchControl Members

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewSearchContract.EnableFiltering;
            }
            set
            {
                radGridViewSearchContract.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
             get
            {
                DataSetFSO.VIEW_SEARCH_CONTRACTRow dr;
                DataRow returnRow;
                if (radGridViewSearchContract.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.VIEW_SEARCH_CONTRACTRow)(((DataRowView)(radGridViewSearchContract.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = contractTableAdapter1.GetDataByID(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewSearchContract.SelectedRows;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set 
            {
                _isNotFill = false;
                dataSetFSO1 = (DataSetFSO)value; 
                InitializeGridView(); 
            }
        }

        public void RefreshList()
        {
            //Обнуляем датасорс, иначе зависает прога, что-то связанное с иерархией (если ее убрать, то обновление нормально проходит)
            radGridViewSearchContract.DataSource = null;

            vieW_SEARCH_CONTRACTTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_CONTRACT);
            InitializeGridView();
        }

        #endregion

        #region Events

        private void UserControlSearchContract_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                vieW_SEARCH_CONTRACTTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_CONTRACT);           
            }

            InitializeGridView();
        }

        private void radGridViewSearchContract_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (this.SelectedRow != null && _openOnDblClick)
            {
                FormContract formContract = new FormContract((DataSetFSO.CONTRACTRow)this.SelectedRow);
                if(!FormHelper.HasOpenedForm(formContract.FormName))
                {
                    formContract.MdiParent = FormHelper.MainForm;
                    formContract.Show();
                    formContract.Select();                    
                }                
            }
        }

        #endregion

    }
}
